package com.java.mybatis.controller;

import com.java.mybatis.entity.Warehouse;
import com.java.mybatis.service.AreaService;
import com.java.mybatis.service.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (Warehouse)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:27
 */
@Controller
@RequestMapping("/warehouse/")
public class WarehouseController {
    /**
     * 服务对象
     */
    @Resource
    private WarehouseService warehouseService;

    @Resource
    private AreaService areaService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Warehouse selectOne(Integer id) {
        return this.warehouseService.queryById(id);
    }

    /**
     * 查询所有仓库的方法
     * @param model
     * @return
     */
    @RequestMapping("getAllWarehouse")
    public String getAllWarehouse(Model model){
        try {
            List<Warehouse> all = warehouseService.getAll();
            model.addAttribute("ware",all);
            Integer wareCount = warehouseService.getWareCount();
            model.addAttribute("count",wareCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/feedback-list";
    }

    /**
     * 禁用仓库修改状态的方法
     * @param id
     * @return
     */
    @RequestMapping("upWareCode")
    @ResponseBody
    public String upWareCode(Integer id,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
       response.setContentType("UTF-8");
        try {
            Integer integer = areaService.selAreaCode(id);

            if(integer==0){
                warehouseService.upwareCode(id);
            }else {
                return "选择的仓库中含有货物地区未停用的仓库，不能被禁用。";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "禁用成功！";
    }

    /**
     * 启用仓库修改状态的方法
     * @param id
     * @return
     */
    @RequestMapping("upwarehouseCode")
    public String upwarehouseCode(Integer id){
        try {
            warehouseService.upwarehouseCode(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/warehouse/getAllWarehouse";
    }

    @RequestMapping("getWareCodeAll")
    @ResponseBody
    public String getWareCodeAll(String str,HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("UTF-8");
        String[] split = str.split(",");
        for (String s : split) {
            Integer integer = areaService.selAreaCode(Integer.parseInt(s));
            if(integer==0){
                warehouseService.upwareCode(Integer.parseInt(s));
            }else {
                return "该仓库下还有货物地区正在使用，不能被禁用，请重新选择。";
            }
        }
        return "禁用成功！";
    }
}